package widget

import (
	"image/color"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
)

//type BorderedMulitContainer struct {
//	content fyne.Widget // ❌ 原来是 CanvasObject，现在改为 Widget
//	rect    *canvas.Rectangle
//}

type BorderedMultiContainer struct {
	contents []fyne.CanvasObject // 存储多个子控件
	rect     *canvas.Rectangle
}

func NewBorderedMultiContainer(contents ...fyne.CanvasObject) *BorderedMultiContainer {
	return &BorderedMultiContainer{
		contents: contents,
		rect: &canvas.Rectangle{
			FillColor:   color.Transparent,                      // 背景透明
			StrokeColor: color.RGBA{R: 255, G: 0, B: 0, A: 255}, // 红色边框
			StrokeWidth: 3,
		},
	}
}

//	func (c *BorderedMulitContainer) CreateRenderer() fyne.WidgetRenderer {
//		return &borderedMultiContainerRenderer{c, c.rect, c.content.CreateRenderer()}
//	}
func (c *BorderedMultiContainer) CreateRenderer() fyne.WidgetRenderer {
	o := &borderedMultiContainerRenderer{
		container: c,
		rect:      c.rect,
		objects:   c.contents,
	}
	return o
}

// Refresh 手动触发刷新
//
//	func (c *BorderedMulitContainer) Refresh() {
//		c.content.Refresh()
//	}
func (c *BorderedMultiContainer) Refresh() {
	for _, content := range c.contents {
		content.Refresh()
	}
}

// MinSize 获取最小尺寸
//
//	func (c *BorderedMulitContainer) MinSize() fyne.Size {
//		return c.content.MinSize()
//	}
func (c *BorderedMultiContainer) MinSize() fyne.Size {
	for _, content := range c.contents {
		minSize := content.MinSize()
		if minSize.Width > 0 && minSize.Height > 0 {
			return minSize.Add(fyne.NewSize(6, 6)) // 增加边框宽度
		}
	}
	return fyne.Size{}
}

// Move 设置位置
//
//	func (c *BorderedMulitContainer) Move(pos fyne.Position) {
//		c.content.Move(pos)
//	}
func (c *BorderedMultiContainer) Move(pos fyne.Position) {
	for _, content := range c.contents {
		content.Move(pos)
	}
}

// Position 获取位置
//
//	func (c *BorderedMulitContainer) Position() fyne.Position {
//		return c.content.Position()
//	}
func (c *BorderedMultiContainer) Position() fyne.Position {
	for _, content := range c.contents {
		pos := content.Position()
		if pos.X > 0 && pos.Y > 0 {
			return pos
		}
	}
	return fyne.Position{}
}

// Resize 调整大小
//
//	func (c *BorderedMulitContainer) Resize(size fyne.Size) {
//		c.content.Resize(size)
//		c.rect.Resize(size)
//	}
func (c *BorderedMultiContainer) Resize(size fyne.Size) {
	for _, content := range c.contents {
		content.Resize(size)
	}
	c.rect.Resize(size)
}

// Show 显示
//
//	func (c *BorderedMulitContainer) Show() {
//		c.content.Show()
//		c.rect.Show()
//	}
func (c *BorderedMultiContainer) Show() {
	for _, content := range c.contents {
		content.Show()
	}
	c.rect.Show()
}

// Hide 隐藏
//
//	func (c *BorderedMulitContainer) Hide() {
//		c.content.Hide()
//		c.rect.Hide()
//	}
func (c *BorderedMultiContainer) Hide() {
	for _, content := range c.contents {
		content.Hide()
	}
	c.rect.Hide()
}

// Visible 是否可见
//
//	func (c *BorderedMulitContainer) Visible() bool {
//		return c.content.Visible()
//	}
func (c *BorderedMultiContainer) Visible() bool {
	for _, content := range c.contents {
		if !content.Visible() {
			return false
		}
	}
	return false
}

// Size 获取当前尺寸
//
//	func (c *BorderedMulitContainer) Size() fyne.Size {
//		return c.content.Size()
//	}
func (c *BorderedMultiContainer) Size() fyne.Size {
	for _, content := range c.contents {
		if size := content.Size(); size.Width > 0 && size.Height > 0 {
			return size
		}
	}
	return fyne.Size{}
}

// Destroy 销毁
func (c *BorderedMultiContainer) Destroy() {}

// ----------------------------
// 渲染器部分
// ----------------------------

//	type borderedMultiContainerRenderer struct {
//		container *BorderedMulitContainer
//		rect      *canvas.Rectangle
//		renderer  fyne.WidgetRenderer
//	}
type borderedMultiContainerRenderer struct {
	container *BorderedMultiContainer
	rect      *canvas.Rectangle
	objects   []fyne.CanvasObject
}

//	func (r *borderedMultiContainerRenderer) Layout(size fyne.Size) {
//		r.renderer.Layout(size)
//		r.rect.Resize(size)
//	}
func (r *borderedMultiContainerRenderer) Layout(size fyne.Size) {
	r.rect.Resize(size)

	// 计算并设置每个子控件的位置和大小
	var contentSize fyne.Size
	if len(r.objects) > 0 {
		contentSize = r.objects[0].MinSize()
		for _, obj := range r.objects[1:] {
			contentSize = contentSize.Max(obj.MinSize())
		}
		contentSize = contentSize.Max(size)
	}

	for _, obj := range r.objects {
		obj.Resize(contentSize)
		obj.Move(fyne.NewPos(0, 0)) // 这里可以调整子控件的具体布局
	}
}

//	func (r *borderedMultiContainerRenderer) MinSize() fyne.Size {
//		return r.container.MinSize()
//	}
func (r *borderedMultiContainerRenderer) MinSize() fyne.Size {
	minSize := fyne.NewSize(0, 0)
	for _, obj := range r.objects {
		minSize = minSize.Max(obj.MinSize())
	}
	return minSize.Add(fyne.NewSize(6, 6)) // 增加边框宽度
}

//	func (r *borderedMultiContainerRenderer) Refresh() {
//		r.renderer.Refresh()
//		r.rect.Refresh()
//	}
func (r *borderedMultiContainerRenderer) Refresh() {
	for _, obj := range r.objects {
		obj.Refresh()
	}
	r.rect.Refresh()
}

//func (r *borderedMultiContainerRenderer) Objects() []fyne.CanvasObject {
//	// 创建一个新的切片，第一个元素是红色边框 rect
//	// 然后用 append 把原始控件的渲染对象追加进去
//	objects := append([]fyne.CanvasObject{r.rect}, r.renderer.Objects()...)
//	return objects
//}

func (r *borderedMultiContainerRenderer) Objects() []fyne.CanvasObject {
	// 创建一个新的切片，第一个元素是红色边框 rect
	// 然后用 append 把原始控件的渲染对象追加进去
	objects := make([]fyne.CanvasObject, 0, len(r.objects)+1)
	objects = append(objects, r.rect)
	objects = append(objects, r.objects...)
	return objects
}

//func (r *borderedMultiContainerRenderer) Destroy() {
//	r.renderer.Destroy()
//}

func (r *borderedMultiContainerRenderer) Destroy() {
	for _, obj := range r.objects {
		if d, ok := obj.(interface{ Destroy() }); ok {
			d.Destroy()
		}
	}
}
